﻿using System.Collections.Generic;
using System.Linq;
using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Core.Service.Utils;

namespace MRGSP.SurveyMaster.Service.Utils
{
    public class RadioReader : IValueReader
    {
        public IEnumerable<Answer> Read(Question question, IDictionary<string, string> f)
        {
            if (!f.ContainsKey("c" + question.ParentId)) yield break;
            var value = f["c" + question.ParentId];
            if (value != question.Id.ToString()) yield break;

            yield return new Answer { QuestionId = question.Id };

            foreach (var q in question.Children.SelectMany(child => ValueReaderFactory.Get(child.Type).Read(child, f)))
                yield return q;
        }
    }
}